package com.clock.shop.service.impl;

import java.util.List;

import com.clock.common.enums.shop.CouponType;
import com.clock.shop.domain.ShopCoupon;
import com.clock.shop.domain.ShopShop;
import com.clock.shop.domain.dto.UserCouponDTO;
import com.clock.shop.domain.vo.ShopShopVo;
import com.clock.shop.mapper.ShopCouponMapper;
import com.clock.shop.service.IShopCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 优惠券Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class ShopCouponServiceImpl implements IShopCouponService
{
    @Resource
    private ShopCouponMapper shopCouponMapper;

    @Autowired
    private ShopShopServiceImpl shopShopService;
    /**
     * 查询优惠券
     * 
     * @param couponId 优惠券主键
     * @return 优惠券
     */
    @Override
    public ShopCoupon selectShopCouponByCouponId(Long couponId)
    {
        return shopCouponMapper.selectShopCouponByCouponId(couponId);
    }

    /**
     * 查询优惠券列表
     * 
     * @param shopCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<ShopCoupon> selectShopCouponList(ShopCoupon shopCoupon)
    {
        // 根据店铺名查询店铺信息获取店铺id
        if(shopCoupon.getShopName() != null){
            List<ShopShop> shopShops = shopShopService.selectShopShopByShopName(shopCoupon.getShopName());
            shopCoupon.setShopId(shopShops.get(0).getShopId());
        }
        // 查询店铺优惠券
        List<ShopCoupon> shopCoupons = shopCouponMapper.selectShopCouponList(shopCoupon);
        for (ShopCoupon shopCoupon1 : shopCoupons
             ) {
            // 根据店铺id查询店铺信息
            ShopShopVo shopShopVo = shopShopService.selectShopShopByShopId(shopCoupon1.getShopId());
            // 设置店铺名
            shopCoupon1.setShopName(shopShopVo.getShopName());
            // 设置优惠券类型
            if(shopCoupon1.getCouponType().equals(CouponType.DISCOUNT.getCode())){
                shopCoupon1.setCouponType(CouponType.DISCOUNT.getInfo());
            }else if(shopCoupon1.getCouponType().equals(CouponType.SPENDINGENOUGH.getCode())){
                shopCoupon1.setCouponType(CouponType.SPENDINGENOUGH.getInfo());
            }else {
                shopCoupon1.setCouponType(CouponType.MEMBERDISCOUNT.getInfo());
            }
        }
        return shopCoupons;
    }

    /**
     * 查询用户个人优惠券包
     * @param userCouponDTO 参数接收实体
     * @return 优惠券集合
     */
    @Override
    public List<ShopCoupon> selectUserCouponList(UserCouponDTO userCouponDTO) {
        // 判断是否是查询指定店铺优惠券
        if(userCouponDTO.getShopName() != null){
            List<ShopShop> shopShops = shopShopService.selectShopShopByShopName(userCouponDTO.getShopName());
            userCouponDTO.setShopId(shopShops.get(0).getShopId());
        }
        return shopCouponMapper.selectUserCouponList(userCouponDTO);
    }

    /**
     * 新增优惠券
     * 
     * @param shopCoupon 优惠券
     * @return 结果
     */
    @Override
    public int insertShopCoupon(ShopCoupon shopCoupon)
    {
        // 根据店铺名查询店铺信息获取店铺id
        if(shopCoupon.getShopName() != null){
            List<ShopShop> shopShops = shopShopService.selectShopShopByShopName(shopCoupon.getShopName());
            shopCoupon.setShopId(shopShops.get(0).getShopId());
        }
        return shopCouponMapper.insertShopCoupon(shopCoupon);
    }

    /**
     * 修改优惠券
     * 
     * @param shopCoupon 优惠券
     * @return 结果
     */
    @Override
    public int updateShopCoupon(ShopCoupon shopCoupon)
    {
        // 根据店铺名查询店铺信息获取店铺id
        if(shopCoupon.getShopName() != null){
            List<ShopShop> shopShops = shopShopService.selectShopShopByShopName(shopCoupon.getShopName());
            shopCoupon.setShopId(shopShops.get(0).getShopId());
        }
        return shopCouponMapper.updateShopCoupon(shopCoupon);
    }

    /**
     * 批量删除优惠券
     * 
     * @param couponIds 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteShopCouponByCouponIds(Long[] couponIds)
    {
        return shopCouponMapper.deleteShopCouponByCouponIds(couponIds);
    }

    /**
     * 删除优惠券信息
     * 
     * @param couponId 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteShopCouponByCouponId(Long couponId)
    {
        return shopCouponMapper.deleteShopCouponByCouponId(couponId);
    }

    /**
     * 查询用户可兑换优惠券列表
     * @param shopCoupon 优惠券
     * @return 可兑换优惠券列表
     */
    @Override
    public List<ShopCoupon> selectUserCanExchangeCouponList(ShopCoupon shopCoupon) {
        return shopCouponMapper.selectUserCanExchangeCouponList(shopCoupon);
    }
}
